package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import util.GetTime;

public class CommunityDAO {
	
	//커뮤니티 등록
	public boolean insert(Community comm)  {
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			/*@@ PreparedStatement pstmt = conn.prepareStatement(
					"insert into community(coname, coopen, coregdate, cologoofn," +
					" cologosfn, copoptext, copopwidth, copopheight, coguide, ccno)" +
					" values(?,?,now(),?,?,?,?,?,?,?)");*/
			PreparedStatement pstmt = conn.prepareStatement(
					"insert into community(coname, coopen, coregdate, cologoofn," +//4
					" cologosfn, copoptext, copopwidth, copopheight, coguide, ccno, cono)" +//6
					" values(?,?,?,?,?,?,?,?,?,?,COSEQ.NEXTVAL)");
			
			pstmt.setString(1, comm.getConame());
			pstmt.setInt(2, comm.getCoopen());
			pstmt.setTimestamp(3, GetTime.nowTime);
			pstmt.setString(4, comm.getCologoofn());
			pstmt.setString(5, comm.getCologosfn());
			pstmt.setString(6, comm.getCopoptext());
			pstmt.setInt(7, comm.getCopopwidth());
			pstmt.setInt(8, comm.getCopopheight());
			pstmt.setString(9, comm.getCoguide());
			pstmt.setInt(10, comm.getCcno());
			
			System.out.println("comm.getConame() = "+comm.getConame()+
					" comm.getCoopen() = "+comm.getCoopen()+" GetTime.nowTime = "+ GetTime.nowTime +
					" comm.getCologoofn() = "+comm.getCologoofn()+" comm.getCologosfn() = "+comm.getCologosfn()+
					" comm.getCopoptext() = "+comm.getCopoptext()+" comm.getCoguide() = "+comm.getCoguide()+
					" comm.getCcno() = "+comm.getCcno());
			pstmt.executeUpdate();
			///////////////////////////////////////////////
			return true;
		} catch(Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
	}
	
	//커뮤니티 공개여부 수정
	public boolean updateCoOpen(Community comm) {
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			PreparedStatement pstmt = conn.prepareStatement(
					"update community set coopen=? " +
					"where cono=?");

			pstmt.setInt(1, comm.getCoopen());
			pstmt.setInt(2, comm.getCono());
			
			pstmt.executeUpdate();			
			///////////////////////////////////////////////
			return true;
		} catch(Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
	}

	//커뮤니티 등록일 수정
	public boolean setCommRegistDate(int cono) {
		String sql = "update community set coregdate=now() where cono="+cono;
		boolean result = false;
		Connection conn = null; 
		try {
			conn = ConnectionManager.getConnection();
			PreparedStatement pstmt = conn.prepareStatement(sql);
			int rst = pstmt.executeUpdate();
			if(rst>0) result = true;			
		} catch (Exception e){
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {			
				e.printStackTrace();
			}
		}
		
		return result;
	}
	
	
	//커뮤니티 로고 수정
	public boolean updateCoLogo(Community comm) {
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			PreparedStatement pstmt = conn.prepareStatement(
					"update community set cologoofn=?, cologosfn=?" +
					"where cono=?");
			pstmt.setString(1, comm.getCologoofn());
			pstmt.setString(2, comm.getCologosfn());
			pstmt.setInt(3, comm.getCono());
			
			pstmt.executeUpdate();			
			///////////////////////////////////////////////
			return true;
		} catch(Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
	}

	//커뮤니티 팝업 수정
	public boolean updateCoPop(Community comm) {
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			PreparedStatement pstmt = conn.prepareStatement(
					"update community set copoptext=?, copopwidth=?, copopheight=? " +
					"where cono=?");
			pstmt.setString(1, comm.getCopoptext());
			pstmt.setInt(2, comm.getCopopwidth());
			pstmt.setInt(3, comm.getCopopheight());
			pstmt.setInt(4, comm.getCono());
			
			pstmt.executeUpdate();			
			///////////////////////////////////////////////
			return true;
		} catch(Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
	}

	//커뮤니티 소개 수정
	public boolean updateCoGudie(Community comm) {
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			PreparedStatement pstmt = conn.prepareStatement(
					"update community set coguide=?" +
					"where cono=?");
			pstmt.setString(1, comm.getCoguide());
			pstmt.setInt(2, comm.getCono());
			
			pstmt.executeUpdate();			
			///////////////////////////////////////////////
			return true;
		} catch(Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
	}
	
	//커뮤니티 삭제
	public boolean delete(int cono) {
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			Statement stmt = conn.createStatement();
			stmt.executeUpdate("delete from community where cono=" + cono);
			///////////////////////////////////////////////
			return true;
		} catch(Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
	}
	
	
	
	//특정 커뮤니티 정보 얻어오기
	public Community getCommunity(Community comm){
		Community community = null;
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("select * from community where cono=" + comm.getCono());
			
			if(rs.next()) {
				community = new Community();
				community.setCono(rs.getInt(1));
				community.setConame(rs.getString(2));
				community.setCoopen(rs.getInt(3));
				community.setCoregdate(rs.getDate(4));
				community.setCologoofn(rs.getString(5));
				community.setCologosfn(rs.getString(6));
				community.setCopoptext(rs.getString(7));
				community.setCopopwidth(rs.getInt(8));
				community.setCopopheight(rs.getInt(9));
				community.setCoguide(rs.getString(10));
				community.setCcno(rs.getInt(11));
			}
			rs.close();
			stmt.close();
			///////////////////////////////////////////////
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return community;
	}	
	
	
	//해당커뮤니티키 취득 (조성일이 만듬)
	public String getCommunityNo(String coname){
		String cono = null;
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("" +
					"select cono from community " +
					"where coname='"+coname+"'");
			
			while(rs.next()) {
				
				cono = rs.getString(1);
			}
			rs.close();
			stmt.close();
			///////////////////////////////////////////////
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return cono;
	}
	
	//특정 커뮤니티 명 얻어 오기
	public String getCommunityName(int cono)
	{
		String coname = null;
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("" +
					"select coname from community " +
					"where cono="+cono);
			
			while(rs.next()) {
				
				coname = rs.getString(1);
			}
			rs.close();
			stmt.close();
			///////////////////////////////////////////////
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return coname;
	}
	
	
	//조성일 제작
	public Vector getHoldonCommunityList(){
		Vector vector = new Vector();
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			Statement stmt = conn.createStatement();
			String sql = "SELECT c1.* , c2.mid , c2.mcnick FROM community c1 , membercommunity c2 where c1.cono = c2.cono and coopen = 0 or coopen=1";
			ResultSet rs = stmt.executeQuery(sql);
			//mid , mcnick
			
			while(rs.next()) {
				Community community = new Community();
				community.setCono(rs.getInt(1));
				community.setConame(rs.getString(2));
				community.setCoopen(rs.getInt(3));
				community.setCoregdate(rs.getDate(4));
				community.setCologoofn(rs.getString(5));
				community.setCologosfn(rs.getString(6));
				community.setCopoptext(rs.getString(7));
				community.setCopopwidth(rs.getInt(8));
				community.setCopopheight(rs.getInt(9));
				community.setCoguide(rs.getString(10));
				community.setCcno(rs.getInt(11));
	
				//조성일 추가 부분
				community.setMasterid(rs.getString(12));
				community.setMasternickname(rs.getString(13));
				
				vector.add(community);
			}
			rs.close();
			stmt.close();
			///////////////////////////////////////////////
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return vector;
	}
	
	/**
	 *조성일 제작
	 *현재 대기중인 커뮤니티 건수 
	 * */
	public int getHoldonCommunityCount(){
		
		int result = 0;
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			Statement stmt = conn.createStatement();
			String sql = "SELECT count(*) FROM community c1 , membercommunity c2 where c1.cono = c2.cono and coopen = 0 or coopen=1";
			ResultSet rs = stmt.executeQuery(sql);
			//mid , mcnick
			
			while(rs.next()) {
				result = rs.getInt(1);
			}
			rs.close();
			stmt.close();
			///////////////////////////////////////////////
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return result;
	}
	
	
	//조성일이 만듬 커뮤니티 정보 추출용
	public Community getCommunity(String cono){
		Community community = null;
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			Statement stmt = conn.createStatement();
			String sql = "SELECT c1.* , c2.mid , c2.mcnick FROM community c1 , membercommunity c2 where c1.cono = c2.cono and c1.cono="+cono;
			
			ResultSet rs = stmt.executeQuery(sql);
			
			if(rs.next()) {
				community = new Community();
				community.setCono(rs.getInt(1));
				community.setConame(rs.getString(2));
				community.setCoopen(rs.getInt(3));
				community.setCoregdate(rs.getDate(4));
				community.setCologoofn(rs.getString(5));
				community.setCologosfn(rs.getString(6));
				community.setCopoptext(rs.getString(7));
				community.setCopopwidth(rs.getInt(8));
				community.setCopopheight(rs.getInt(9));
				community.setCoguide(rs.getString(10));
				community.setCcno(rs.getInt(11));
	
				//조성일 추가 부분
				community.setMasterid(rs.getString(12));
				community.setMasternickname(rs.getString(13));
			}
			rs.close();
			stmt.close();
			///////////////////////////////////////////////
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return community;
	}	
	

	//신설동호회 3개 가져오는 메소드
	public Vector<Community> getNewCommunityList(){
		Vector<Community> vector = new Vector<Community>();
		Connection conn = ConnectionManager.getConnection();
		int count = 0;//##
		try {
			///////////////////////////////////////////////
			Statement stmt = conn.createStatement();
			/*@@ ResultSet rs = stmt.executeQuery("" +
					"select count(*) count, co.* from " +
					"membercommunity mc, community co " +
					"where mc.cono=co.cono and coopen=2 and co.cono>9 " +
					"group by mc.cono order by coregdate desc,cono desc limit 3");*/
			
			//*#
			String sql = "select count(*) count, co.* from membercommunity mc, community co " +
					"where mc.cono=co.cono and coopen=2 and co.cono>9 " +
					"group by co.CONO,co.CONAME,co.COOPEN,co.COREGDATE,co.COLOGOOFN,co.COLOGOSFN,co.COPOPTEXT,co.COPOPWIDTH,co.COPOPHEIGHT,co.COGUIDE,co.CCNO " +
					"order by coregdate desc ";
			ResultSet rs = stmt.executeQuery(sql);
		
			while(rs.next()) {
				count++;//##
				if(count >=1 && count <=3){//##
					Community community = new Community();
					
					community.setMembercount(rs.getInt(1));
					
					community.setCono(rs.getInt(2));
					community.setConame(rs.getString(3));
					community.setCoopen(rs.getInt(4));
					community.setCoregdate(rs.getDate(5));
					community.setCologoofn(rs.getString(6));
					community.setCologosfn(rs.getString(7));
					community.setCopoptext(rs.getString(8));
					community.setCopopwidth(rs.getInt(9));
					community.setCopopheight(rs.getInt(10));
					community.setCoguide(rs.getString(11));
					community.setCcno(rs.getInt(12));				
					
					vector.add(community);
				}//##
			}
			rs.close();
			stmt.close();
			///////////////////////////////////////////////
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return vector;
	}
	//위 코드 다시.
	/*public Vector<Community> getNewCommunityList(){
		Vector<Community> vector = new Vector<Community>();
		Connection conn = ConnectionManager.getConnection();
		int count = 0;//##
		try {
			///////////////////////////////////////////////
			Statement stmt = conn.createStatement();
			@@ ResultSet rs = stmt.executeQuery("" +
					"select count(*) count, co.* from " +
					"membercommunity mc, community co " +
					"where mc.cono=co.cono and coopen=2 and co.cono>9 " +
					"group by mc.cono order by coregdate desc,cono desc limit 3");
			
			//*#
			String sql = "select * from community ";
		    sql+= "where coopen=2 and cono>9 and cono IN(select cono from membercommunity) ";
		    sql += "order by coregdate desc, cono desc" ;           
			ResultSet rs = stmt.executeQuery(sql);
			
			while(rs.next()) {
				count++;//##
				if(count >=1 && count <=3){//##
					Community community = new Community();
					
					String sql1 = "select count(*)from membercommunity where cono=?";
					PreparedStatement pstmt = conn.prepareStatement(sql1);
					pstmt.setInt(1, rs.getInt("cono"));
					ResultSet rs1 = pstmt.executeQuery();
					rs1.next();
					community.setMembercount(rs1.getInt(1));
					
					community.setCono(rs.getInt("cono"));
					community.setConame(rs.getString("coname"));
					community.setCoopen(rs.getInt("coopen"));
					community.setCoregdate(rs.getDate("coregdate"));
					community.setCologoofn(rs.getString("cologoofn"));
					community.setCologosfn(rs.getString("cologosfn"));
					community.setCopoptext(rs.getString("copoptext"));
					community.setCopopwidth(rs.getInt("copopwidth"));
					community.setCopopheight(rs.getInt("copopheight"));
					community.setCoguide(rs.getString("coguide"));
					community.setCcno(rs.getInt("ccno"));				
					System.out.println("@@@@@@cono" + community.getCono());
					System.out.println("@@@@@@@@mcCount" + community.getMembercount());
					vector.add(community);
				}//##
			}
			rs.close();
			stmt.close();
			///////////////////////////////////////////////
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return vector;
	}*/

	//가장 최근 만들어진 동호회 가져오기
	public Community getNewCommunity(){
		Community community = new Community();
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			Statement stmt = conn.createStatement();
			/*ResultSet rs = stmt.executeQuery("select * from community " +
					"where coopen=2 and cono>9 order by coregdate desc,cono desc limit 1");*/
			ResultSet rs = stmt.executeQuery("select * from community " +
			"where coopen=2 and cono>9 order by coregdate desc,cono desc ");
			
	//	while(rs.next()) {
		rs.next();
				community.setCono(rs.getInt(1));
				community.setConame(rs.getString(2));
				community.setCoopen(rs.getInt(3));
				community.setCoregdate(rs.getDate(4));
				community.setCologoofn(rs.getString(5));
				community.setCologosfn(rs.getString(6));
				community.setCopoptext(rs.getString(7));
				community.setCopopwidth(rs.getInt(8));
				community.setCopopheight(rs.getInt(9));
				community.setCoguide(rs.getString(10));
				community.setCcno(rs.getInt(11));
		//	}
			rs.close();
			stmt.close();
			///////////////////////////////////////////////
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return community;
	}

	//신청 들어간 커뮤니티 가져오기
	public Community getNewCreateCommunity(){
		Community community = new Community();
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			Statement stmt = conn.createStatement();
			/*@@ ResultSet rs = stmt.executeQuery("select * from community " +
					"where coopen in (0,1) and cono>9 order by coregdate desc,cono desc limit 1");*/
			ResultSet rs = stmt.executeQuery("select * from community " +
			"where coopen in (0,1) and cono>9 order by coregdate desc,cono desc");
			while(rs.next()) {				
				community.setCono(rs.getInt(1));
				community.setConame(rs.getString(2));
				community.setCoopen(rs.getInt(3));
				community.setCoregdate(rs.getDate(4));
				community.setCologoofn(rs.getString(5));
				community.setCologosfn(rs.getString(6));
				community.setCopoptext(rs.getString(7));
				community.setCopopwidth(rs.getInt(8));
				community.setCopopheight(rs.getInt(9));
				community.setCoguide(rs.getString(10));
				community.setCcno(rs.getInt(11));
			}
			rs.close();
			stmt.close();
			///////////////////////////////////////////////
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return community;
	}

	//인기커뮤니티 3개 가져오기
	public Vector<Community> getPopulCommunityList(){
		Vector<Community> vector = new Vector<Community>();
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			Statement stmt = conn.createStatement();
			int count = 0;//##
			/*@@ResultSet rs = stmt.executeQuery("select count(*) count, co.* from membercommunity mc, community co " +
			     "where mc.cono=co.cono and coopen=2 and co.cono>9 group by mc.cono order by count desc");*/
			//*#
			ResultSet rs = stmt.executeQuery("select count(*) count, co.* from membercommunity mc, community co " +
					"where mc.cono=co.cono and coopen=2 and co.cono>9 " +
					"group by co.CONO,co.CONAME,co.COOPEN,co.COREGDATE,co.COLOGOOFN,co.COLOGOSFN,co.COPOPTEXT,co.COPOPWIDTH,co.COPOPHEIGHT,co.COGUIDE,co.CCNO " +
					"order by count desc");
			//#*
			while(rs.next()) {
				count++;//##
				if(count >=1 && count <=3){//##
					Community community = new Community();
					
					community.setMembercount(rs.getInt(1));
					
					community.setCono(rs.getInt(2));
					community.setConame(rs.getString(3));
					community.setCoopen(rs.getInt(4));
					community.setCoregdate(rs.getDate(5));
					community.setCologoofn(rs.getString(6));
					community.setCologosfn(rs.getString(7));
					community.setCopoptext(rs.getString(8));
					community.setCopopwidth(rs.getInt(9));
					community.setCopopheight(rs.getInt(10));
					community.setCoguide(rs.getString(11));
					community.setCcno(rs.getInt(12));
					
					vector.add(community);
				}
			}
			rs.close();
			stmt.close();
			///////////////////////////////////////////////
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return vector;
	}

	//카테고리별 커뮤니티 가져오기
	public Vector<Community> getSortCommunityList(){
		  Vector<Community> vector = new Vector<Community>();
		  Connection conn = ConnectionManager.getConnection();
		  try {
		   ///////////////////////////////////////////////
		   Statement stmt = conn.createStatement();
		   ResultSet rs = stmt.executeQuery("" +
		     "select ccno, ccname from communitycategory where ccno != 1 order by ccno");
		   
		   /*@@ String sql = "select count(*) count, co.ccno, ccname " +
		       "from community co, communitycategory cc " +
		       "where co.ccno=cc.ccno and co.ccno=? and " +
		       "coopen=2 and cono>9 group by ccno";*/
		   String sql = "select count(*) count, co.ccno, ccname " +
		        "from community co, communitycategory cc " +
		        "where co.ccno=cc.ccno and co.ccno=? and coopen=2 and cono>9"+
		        "group by co.ccno, ccname";
		   PreparedStatement pstmt = conn.prepareStatement(sql);
		   ResultSet rs2 = null;
		   while(rs.next()) {
		    Community community = new Community();
		    pstmt.setInt(1, rs.getInt(1));
		    rs2 = pstmt.executeQuery();
		    
		    if(rs2.next()){
		     community.setCcnocount(rs2.getInt(1));
		     community.setCcno(rs2.getInt(2));
		     community.setCcname(rs2.getString(3));
		     vector.add(community);
		    }else{
		     community.setCcnocount(0);
		     community.setCcno(rs.getInt(1));
		     community.setCcname(rs.getString(2));
		    }  
		    
		    
		   }
		   rs.close();
		   rs2.close();
		   stmt.close();
		   ///////////////////////////////////////////////
		  } catch(Exception e) {
		   e.printStackTrace();
		  } finally {
		   if(conn!=null) try { conn.close(); } catch(Exception e) {}
		  }
		  return vector;
		 } 
	
	/*
	 * 이건 내가 만들었음.
	 * */
	public Vector getSearchCommunityList(){
		
		Vector vector = new Vector();
		
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("SELECT * FROM community where coopen != 0 and coopen != 1");
			
			while(rs.next()) {
		
				Community community = new Community();
				community.setCono(rs.getInt(1));
				community.setConame(rs.getString(2));
				vector.add(community);
			}
			rs.close();
			stmt.close();
			///////////////////////////////////////////////
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return vector;
	}	
	
	//커뮤니티 전체 개수 얻어오기
	public long getTotalRows() {
		long result = 0;
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("select count(*) from community where cono>9 and coopen!=0 and coopen!=1");	//커뮤니티 전체수(대기중인 커뮤니티 제외)
			System.out.println();
			rs.next();
			result = rs.getLong(1);
			rs.close();
			stmt.close();
			///////////////////////////////////////////////
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return result;
	}

	//특정 카테코리의 커뮤니티 갯수 얻어오기
	public long getTotalRows(int ccno) {
		long result = 0;
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			Statement stmt = conn.createStatement();
			ResultSet rs = null;
			if(ccno!=0){
				rs = stmt.executeQuery("select count(*) from community " +
						"where ccno="+ccno+" and coopen=2 and cono>9");
			}else{
				rs = stmt.executeQuery("select count(*) from community where coopen=2 and cono>9");
			}
			rs.next();
			result = rs.getLong(1);
			rs.close();
			stmt.close();
			///////////////////////////////////////////////
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return result;
	}

	//특정 문자열을 포함한 커뮤니티 갯수 가져오기
	public long getTotalRows(String coname) {
		long result = 0;
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			Statement stmt = conn.createStatement();
			ResultSet rs = null;
			rs = stmt.executeQuery("select count(*) from community " +
						"where coname like '%"+coname+"%' and coopen=2 and cono>9");
			rs.next();
			result = rs.getLong(1);
			rs.close();
			stmt.close();
			///////////////////////////////////////////////
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return result;
	}
	
	//특정 커뮤니티 명 가져오기
	public String getCommuniytName(int cono){
		String coname = null;
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("select coname from community " +
					"where cono="+cono);
			rs.next();
			coname =rs.getString(1);
			rs.close();
			stmt.close();
			///////////////////////////////////////////////
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return coname;
	}
	
	//특정 문자열을 포함한 커뮤니티 정보 가져오기
	public Vector<Community> getCommunityList(String coname,long currentPage, long rowsPerPage){
		
		long startindex = (currentPage - 1) * rowsPerPage + 1;
	    long endindex = currentPage * rowsPerPage;
	    System.out.println("currentPage = "+currentPage+" rowsPerPage = "+rowsPerPage);
	    System.out.println("start = "+startindex+" endindex = "+endindex);
	    int index = 0;
		
		Vector<Community> vector = new Vector<Community>();
		Connection conn = null;
		PreparedStatement pstmt = null;
		try{
			conn = ConnectionManager.getConnection();
			/*@@ pstmt = conn.prepareStatement("" +
						"select co.* from " +
						"(select comm.*,(@rownum:=@rownum+1) rownum from " +
						"(select community.*,(@rownum:=0) rnum  from " +
						"community where coname like ? and coopen=2 and cono>9 order by coregdate desc) comm) co limit "+rowsPerPage*(currentPage-1)+","+rowsPerPage);*/
			Statement stmt = conn.createStatement();	
			//@@ pstmt = conn.prepareStatement("select * from community where coname like '"+coname+"' and coopen=2 and cono>9 order by coregdate desc) comm");
			//@@ pstmt.setString(1, coname);
			ResultSet rs = stmt.executeQuery("select * from community where coname like '"+coname+"' and coopen=2 and cono>9 order by coregdate desc");
			//@@ ResultSet rs = pstmt.executeQuery();

			while(rs.next()){
				index++;
				if(index >= startindex && index <= endindex){
				
				
				
				Community community = new Community();
				MemberCommunityDAO mcDAO = new MemberCommunityDAO();
				community.setCono(rs.getInt(1));
				community.setConame(rs.getString(2));
				community.setCoopen(rs.getInt(3));
				community.setCoregdate(rs.getDate(4));
				community.setCologoofn(rs.getString(5));
				community.setCologosfn(rs.getString(6));
				community.setCopoptext(rs.getString(7));
				community.setCopopwidth(rs.getInt(8));
				community.setCopopheight(rs.getInt(9));
				community.setCoguide(rs.getString(10));
				community.setCcno(rs.getInt(11));
				community.setMembercount(mcDAO.countCommunityMembers(community.getCono()));
				community.setMcnick(mcDAO.getCommunityMcnick(community.getCono()));
				community.setMid(mcDAO.getCommunityMid(community.getCono()));
				vector.add(community);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {		
				e.printStackTrace();
			}
		}		
		
		return vector;
	}

	//카테고리별 커뮤니티 정보 가져오기
	public Vector<Community> getCommunityList(int ccno,long currentPage, long rowsPerPage){
		
		long startindex = (currentPage - 1) * rowsPerPage + 1;
	    long endindex = currentPage * rowsPerPage;
	    System.out.println("currentPage = "+currentPage+" rowsPerPage = "+rowsPerPage);
	    System.out.println("start = "+startindex+" endindex = "+endindex);
	    int index = 0;
		
		Vector<Community> vector = new Vector<Community>();
		Connection conn = null;
		PreparedStatement pstmt = null;
		try{
			conn = ConnectionManager.getConnection();
			if(ccno!=0){
				/*@@ pstmt = conn.prepareStatement("" +
						"select co.* from " +
						"(select comm.*,(@rownum:=@rownum+1) rownum from " +
						"(select community.*,(@rownum:=0) rnum  from " +
						"community where ccno=? and coopen=2 and cono>9 order by coregdate desc) comm) co limit "+rowsPerPage*(currentPage-1)+","+rowsPerPage);*/
				
				pstmt = conn.prepareStatement("select * from community where ccno=? and coopen=2 and cono>9 order by coregdate desc");
				pstmt.setInt(1, ccno);
			}else{
				/*@@ pstmt = conn.prepareStatement("" +
						"select co.* from " +
						"(select comm.*,(@rownum:=@rownum+1) rownum from " +
						"(select community.*,(@rownum:=0) rnum  from " +
						"community where coopen=2 and cono>9 order by coregdate desc) comm) co limit "+rowsPerPage*(currentPage-1)+","+rowsPerPage);*/
				
				pstmt = conn.prepareStatement("select * from community where coopen=2 and cono>9 order by coregdate desc");
			}
			ResultSet rs = pstmt.executeQuery();
	
			while(rs.next()){
				
				index++;
				if(index >= startindex && index <= endindex){
				
				Community community = new Community();
				MemberCommunityDAO mcDAO = new MemberCommunityDAO();
				community.setCono(rs.getInt(1));
				community.setConame(rs.getString(2));
				community.setCoopen(rs.getInt(3));
				community.setCoregdate(rs.getDate(4));
				community.setCologoofn(rs.getString(5));
				community.setCologosfn(rs.getString(6));
				community.setCopoptext(rs.getString(7));
				community.setCopopwidth(rs.getInt(8));
				community.setCopopheight(rs.getInt(9));
				community.setCoguide(rs.getString(10));
				community.setCcno(rs.getInt(11));
				community.setMembercount(mcDAO.countCommunityMembers(community.getCono()));
				community.setMcnick(mcDAO.getCommunityMcnick(community.getCono()));
				community.setMid(mcDAO.getCommunityMid(community.getCono()));
				vector.add(community);
				
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {		
				e.printStackTrace();
			}
		}		
		
		return vector;
	}
	
	//커뮤니티의 로고파일명 가져오기
	public String getLogoSaveName(int cono){
		String coname = null;
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("select cologosfn from community " +
					"where cono="+cono);
			rs.next();
			coname =rs.getString(1);
			rs.close();
			stmt.close();
			///////////////////////////////////////////////
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return coname;
	}
	
//	public static void main(String args[]){
//		Community comm = new Community();
//		CommunityDAO commDAO = new CommunityDAO();
//		
////		comm.setCono(1);
//		comm.setConame("좋은 인연");
//		comm.setCoopen(1);
////		comm.setCoregdate();
////		comm.setCologoofn("a");
////		comm.setCologosfn("b");
////		comm.setCopoptext("poptext");
////		comm.setCopopwidth(100);
////		comm.setCopopheight(100);
//		comm.setCoguide("좋은 인연 커뮤니티");
//		comm.setCcno(2);
//		
//		commDAO.insert(comm);
////		commDAO.updateCoGudie(comm);
////		commDAO.updateCoLogo(comm);
////		commDAO.updateCoOpen(comm);
////		commDAO.updateCoPop(comm);
////		commDAO.delete(comm);
//	}

}
